<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="歌曲名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入歌曲名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="歌手" prop="singer">
        <el-input
          v-model="queryParams.singer"
          placeholder="请输入歌手"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="音乐类型" prop="typeData">
        <el-select v-model="queryParams.typeData" placeholder="请选择音乐类型">
            <el-option v-for="item in tvDataOptions" :key="item.id" :label="item.name" :value="item.id" :disabled="item.status == 1"></el-option>
            </el-select>
      </el-form-item>
      <el-form-item label="专辑名称" prop="contentTitle">
        <el-input
          v-model="queryParams.contentTitle"
          placeholder="请输入歌曲名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
                <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
              </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:songData:add']"
        >导入音乐</el-button>
      </el-col> -->
       <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:songData:remove']"
        >删除</el-button>
      </el-col> 
                   <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Download"
          size="mini"
          @click="downTemplate"
          v-hasPermi="['system:songData:remove']"
        >下载模板</el-button>
      </el-col> 
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="songDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="歌曲名称" align="center" prop="title" />
      <el-table-column label="歌手" align="center" prop="singer" />
      <el-table-column label="音乐类型" align="center" prop="typeDataStr" />
      <!-- <el-table-column label="导演" align="center" prop="director" /> -->
      <el-table-column label="专辑名称" align="center" prop="contentTitle" />
      <el-table-column label="专辑图片" align="center" prop="imageFile" >
        <template slot-scope="scope">
          <img v-bind:src="scope.row.imageFile" class="img-lg" style="width: 80px;height: 50px;"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="歌曲地址" align="center" prop="videoFile" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
              </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:songData:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
            v-hasPermi="['system:songData:query']"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改音乐对话框 -->
    <el-dialog :title="title" :visible.sync="openDetail" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
        <el-form-item label="歌曲名称" prop="title">{{ form.title }}
        </el-form-item>
</el-col>
         <!-- <el-col :span="12">
          <el-form-item label="是否推荐" >
          <div v-if="form.isRecommend == 0">不推荐</div>
          <div v-else-if="form.isRecommend == 1">推荐</div>
        </el-form-item>
      </el-col> -->
      <el-col :span="12">
        <el-form-item label="歌手" prop="singer">{{ form.singer }}
        </el-form-item>
        </el-col>

      <el-col :span="12">
        <el-form-item label="音乐类型" prop="typeData">{{ form.typeDataStr }}
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="制作人" prop="director">{{ form.director }}
        </el-form-item>
        </el-col>
      <el-col :span="12">
        <el-form-item label="专辑名称" prop="contentTitle">{{ form.contentTitle }}
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="音乐详情" prop="description">{{ form.description }}
        </el-form-item>
        </el-col>
      <el-col :span="24">
        <el-form-item label="歌曲地址" prop="videoFile">{{ form.videoFile }}
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="歌词地址" prop="songWord">{{ form.songWord }}
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="专辑图片" prop="imgAddress">{{ form.imageFile }}
        </el-form-item>
      </el-col>
      <el-col :span="12">
          <el-form-item label="是否推荐" >
          <div v-if="form.isRecommend == 0">不推荐</div>
          <div v-else-if="form.isRecommend == 1">推荐</div>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="推荐图片" prop="recommendBanner">{{ form.recommendBanner }}
        </el-form-item>
      </el-col>
      <!-- <el-col :span="24">
        <el-form-item label="描述" prop="description">{{ form.description }}
        </el-form-item>
        </el-col> -->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSongData, getSongData, delSongData, addSongData, updateSongData } from "@/api/system/songData";
import { checkUsb,getFileData,getFileDataByPath,getContentType,getContentTypeData } from '@/api/index';

export default {
  name: "SongData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 音乐表格数据
      songDataList: [],
      // 内容分类的类型详情选项
      tvDataOptions: [],      
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openDetail: false,
            // 日期范围
            dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        fileType: 2,//音乐类型
        typeData: null,
        taskId: null,
        userName: null,
        description: null,
        singer: null,
        author: null,
        isRecommend: null,
        status: null,
        num: null,
        videoFile: null,
        imageFile: null,
        keyWord: null,
        songWord: null,
        contentTitle: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getContentTypeData({
          "typeId": this.queryParams.fileType,
         });
  },
  methods: {
    /** 查询音乐列表 */
    getList() {
      this.loading = true;
      // listSongData(this.queryParams).then(response => {
        listSongData(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.songDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
        /** 详细按钮操作 */
    handleView(row) {
      this.openDetail = true;
      this.form = row;
    },
    getContentTypeData(data){
      getContentTypeData(data).then(response =>{
        this.tvDataOptions = response.data;
        }).catch(error => {
          console.log("获取内容分类的类型详情响应:"+error);
          return
        });
    },
    // 取消按钮
    cancel() {
      this.openDetail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        fileType: 1,//电影类型
        typeData: null,
        createTime: null,
        taskId: null,
        userName: null,
        description: null,
        singer: null,
        author: null,
        isRecommend: null,
        updateTime: null,
        status: null,
        num: null,
        videoFile: null,
        imageFile: null,
        keyWord: null,
        songWord: null,
        contentTitle: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDetail = true;
      this.title = "添加音乐";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSongData(id).then(response => {
        this.form = response.data;
        this.openDetail = true;
        this.title = "修改音乐";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSongData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openDetail = false;
              this.getList();
            });
          } else {
            addSongData(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openDetail = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除音乐编号为"' + ids + '"的数据项？').then(function() {
        return delSongData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/songData/export', {
        ...this.queryParams
      }, `songData_${new Date().getTime()}.xlsx`)
    },

    downTemplate(){
      this.download('system/songData/importTemplate', {
      }, `歌曲模板_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
